package cn.wit.summit.chwan.widgets;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.Stat.StatFactory;
import cn.wit.summit.chwan.models.db.DownLoadTaskTable;
import cn.wit.summit.chwan.models.dbmanager.DownloadTaskManager;
import cn.wit.summit.chwan.models.dto.GameListBaseBean;
import cn.wit.summit.chwan.models.response.IndexBeanV3Response;
import cn.wit.summit.chwan.tools.downloadtool.DownLoadStatusConfig;
import cn.wit.summit.chwan.tools.downloadtool.DownloadTool;
import cn.wit.summit.chwan.utils.APKUtils;
import cn.wit.summit.chwan.utils.LogUtils;
import cn.wit.summit.chwan.utils.UtilsMy;


/**
 * Created by pengsk on 2017/10/30.
 */

public class DownloadViewStrokeBig extends LinearLayout implements View.OnClickListener {
    TextView instalButtomButn;
    private TextView moneyText;
    ProgressBar progressBar;
    boolean isDetial=false;
    //    private PayTagInfo payTagInfo;
    private int down_status;
    protected IndexBeanV3Response.CreditInfoBean credit_info;

    public DownloadViewStrokeBig(Context context) {
        super(context);
        initView(context);
    }

    public DownloadViewStrokeBig(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public DownloadViewStrokeBig(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

     void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.install_button_stroke_big_layout, this);
        instalButtomButn = (TextView) view.findViewById(R.id.instalButtomButn);
        moneyText = (TextView) view.findViewById(R.id.moneyText);
        progressBar = view.findViewById(R.id.progressBar);
    }

    protected String gameId;
    private DownLoadTaskTable downloadTask;

    public void initData(String gameid, GameListBaseBean gameListBaseBean) {
//        setCoin(tipNew, downloadTask);
        this.gameId = gameid;
        credit_info = gameListBaseBean.getCredit_info();
//        this.payTagInfo = payTagInfo;
        this.downloadTask = gameListBaseBean.getTaskTable();
        DownLoadTaskTable taskTable = DownloadTaskManager.getInstance().findByGameId(gameid);
        if (taskTable != null) {
            this.downloadTask = taskTable;
        } else if (downloadTask == null) {
            this.downloadTask = gameListBaseBean.getDownloadTaskTableSimple();
        }
        gameListBaseBean.setTaskTable(downloadTask);
        this.down_status = downloadTask.getDownStatus();
        instalButtomButn.setOnClickListener(this);
        progressBar.setOnClickListener(this);
        updateButn(downloadTask);
    }
void setProgressBar(ProgressBar progressBar){
        this.progressBar=progressBar;
}
    public void setProgress(int progress) {
        instalButtomButn.setTextColor(Color.parseColor("#999999"));
        instalButtomButn.setText(progress + "%");
        instalButtomButn.setBackgroundResource(R.drawable.alph);
        progressBar.setVisibility(VISIBLE);
        progressBar.setProgress(progress);

    }

    public void updateButn(DownLoadTaskTable downloadTask) {
        int status = 0;

        if (downloadTask != null) {
            boolean isInstall = APKUtils.getInstance(getContext()).checkInstall(getContext(), downloadTask.getPackageName());
            if (isInstall) downloadTask.setDownStatus(DownLoadStatusConfig.INSTALLED);
            status = downloadTask.getDownStatus();

        }
        progressBar.setVisibility(GONE);
        instalButtomButn.setVisibility(VISIBLE);
        instalButtomButn.setBackgroundResource(R.drawable.installbutn_big_normal_butn);
        instalButtomButn.setTextColor(Color.WHITE);
        if (credit_info != null) {
            instalButtomButn.setText("详情");
            instalButtomButn.setTextColor(Color.parseColor("#262626"));
            instalButtomButn.setBackgroundResource(R.drawable.index_detail_selector);
        } else if (status == DownLoadStatusConfig.UPDATE) {
        } else if (status == DownLoadStatusConfig.UNZIP) {
            instalButtomButn.setText("解压中");
            instalButtomButn.setTextColor(Color.parseColor("#4bbd6e"));
            instalButtomButn.setBackgroundResource(R.drawable.installbutton_unziping_butn);
        } else if (status == DownLoadStatusConfig.UNZIP_ERROR||status==DownLoadStatusConfig.UNZIP_WAIT) {
            instalButtomButn.setText("解压");
            instalButtomButn.setTextColor(Color.WHITE);
            instalButtomButn.setBackgroundResource(R.drawable.installbutton_installandunzip_butn);
        } else if (status == DownLoadStatusConfig.INSTALL_WAIT) {
            instalButtomButn.setText("安装");
            instalButtomButn.setTextColor(Color.WHITE);
            instalButtomButn.setBackgroundResource(R.drawable.installbutton_installandunzip_butn);
        } else if (status == DownLoadStatusConfig.INSTALLING) {
            instalButtomButn.setText("安装中");
            instalButtomButn.setTextColor(Color.WHITE);
            instalButtomButn.setBackgroundResource(R.drawable.installbutton_installandunzip_butn);
        } else if (status == DownLoadStatusConfig.DOWNLOAD_FINISH) {
        } else if (status == DownLoadStatusConfig.DOWNLOADING || status == DownLoadStatusConfig.START) {
            setProgress(downloadTask.getDownprogress());
        } else if (status == DownLoadStatusConfig.DOWNLOAD_STOP || status == DownLoadStatusConfig.DOWNLOAD_ERROR) {
            instalButtomButn.setText("继续");
            instalButtomButn.setTextColor(Color.parseColor("#43BEFE"));
        } else if (status == DownLoadStatusConfig.WAIT) {
            instalButtomButn.setText("等待");
        } else if (status == DownLoadStatusConfig.INSTALLED) {
            instalButtomButn.setTextColor(Color.parseColor("#72b0fd"));
            instalButtomButn.setBackgroundResource(R.drawable.installbutton_open_butn);
            instalButtomButn.setText("打开");
        } else {

            instalButtomButn.setText("立即下载");
            if (downloadTask != null) {
                UtilsMy.setButnStatusBig(instalButtomButn, downloadTask.getDownStatus());
            }
        }
    }
//
//
//    public void instalButtomButnClicked(DownLoadTaskTable downloadTask) {
//
//
//        int status = 0;
//        if (downloadTask != null) {
//            boolean isInstall = APKUtils.getInstance(getContext()).checkInstall(getContext(), downloadTask.getPackageName());
//            if (isInstall) downloadTask.setDownStatus(DownLoadStatusConfig.INSTALLED);
//            status = downloadTask.getDownStatus();
//        }
//        LogUtils.e("statues   " + status + "");
//        if (downloadTask == null) {
//            UtilsMy.starServiceDownGame(getContext(), downloadTask.getGameId());
//            return;
//        } else if (downloadTask != null && (status == DownLoadStatusConfig.DOWNLOADING)) {
//            DownloadTool.stopTask(downloadTask);
//        } else if (status == DownLoadStatusConfig.UNZIP) {
//            return;
//        } else if (status == DownLoadStatusConfig.UNZIP_ERROR||status == DownLoadStatusConfig.UNZIP_WAIT) {
//            DownloadTool.unzip(getContext(), downloadTask);
//            return;
//        } else if (status == DownLoadStatusConfig.INSTALLED) {
//            APKUtils.getInstance(getContext()).openThirdApp(getContext(), downloadTask.getPackageName(),downloadTask.getGameId());
//
//        } else if (DownLoadStatusConfig.INSTALL_WAIT == status) {
//            APKUtils.getInstance(getContext()).installAPK(getContext(), downloadTask.getDownPath(),downloadTask.getGameId());
//        } else {
//            if (downloadTask != null) {
//                DownloadTool.downloadGame(getContext(), downloadTask);
//            }
//        }
//        updateButn(downloadTask);
//    }

    public void instalButtomButnClicked(DownLoadTaskTable downloadTask) {

        int status = 0;
        if (downloadTask != null) {
            boolean isInstall = APKUtils.getInstance(getContext()).checkInstall(getContext(), downloadTask.getPackageName());
            DownLoadTaskTable taskDb = DownloadTaskManager.getInstance().findByGameId(downloadTask.getGameId());
            if (isInstall && taskDb == null && downloadTask.getDownStatus() != DownLoadStatusConfig.UPDATE)
                downloadTask.setDownStatus(DownLoadStatusConfig.INSTALLED);
            status = downloadTask.getDownStatus();

        }

        if (downloadTask == null) {
            UtilsMy.starServiceDownGame(getContext(), downloadTask.getGameId());
            StatFactory.getInstance().sendGameDownload(getContext(),downloadTask.getGameId(),3+"",isDetial);
            return;
        } else if (downloadTask != null && (status == DownLoadStatusConfig.DOWNLOADING)) {
            DownloadTool.stopTask(downloadTask);
            StatFactory.getInstance().sendGameDownload(getContext(),downloadTask.getGameId(),4+"",isDetial);
        } else if (status == DownLoadStatusConfig.UNZIP) {
            return;
        } else if (status == DownLoadStatusConfig.UNZIP_ERROR || status == DownLoadStatusConfig.UNZIP_WAIT) {
            StatFactory.getInstance().sendUnZip(getContext(),downloadTask.getGameId(),3+"");
            DownloadTool.unzip(getContext(), downloadTask);
            return;
        } else if (status == DownLoadStatusConfig.INSTALLED) {
            APKUtils.getInstance(getContext()).openThirdApp(getContext(), downloadTask.getPackageName(), downloadTask.getGameId());

        } else if (DownLoadStatusConfig.INSTALL_WAIT == status) {
            APKUtils.getInstance(getContext()).installAPK(getContext(), downloadTask.getDownPath(), downloadTask.getGameId());
        }
//        else if (status == DownLoadStatusConfig.UPDATE) {
//            if (downloadTask != null) {
//                DownloadTool.downloadGame(getContext(), downloadTask);
//            }
//        }
        else {
            if (downloadTask != null) {
//                StatFactory.getInstance().sendGameDownload(getContext(),downloadTask.getGameId(),3+"",isDetial);
                DownloadTool.downloadGame(getContext(), downloadTask, credit_info, isDetial);
            }
        }
        updateButn(downloadTask);
    }
    long clickTime = 0;
    @Override
    public void onClick(View view) {
        if (clickTime == 0 || System.currentTimeMillis() - clickTime > 2000) {
            clickTime=System.currentTimeMillis();
            instalButtomButnClicked(downloadTask);
        }
    }
}
